package Q3;

import java.util.Arrays;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param numbers int整型一维数组
     * @return bool布尔型
     */
    public static boolean IsContinuous (int[] numbers) {
        // write code here
        Arrays.sort(numbers);

        int curIndex = 0; //指向第一个不为 0 的数
        int zeroCount = 0; //0的个数
        int n = numbers.length;
        while (curIndex < n && numbers[curIndex] == 0) {
            curIndex++;
            zeroCount++;
        }

        curIndex++;

        while (curIndex < n) {
            if (numbers[curIndex] - numbers[curIndex - 1] == 1) { //两数相邻
                curIndex++;
            } else if (numbers[curIndex] - numbers[curIndex - 1] - 1 <= zeroCount) { //两数不相邻，但是有 0 可以替换
                zeroCount -= (numbers[curIndex] - numbers[curIndex - 1] - 1);
                curIndex++;
            } else {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        System.out.println(IsContinuous(new int[]{0, 0, 2, 4, 6}));
    }
}